package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.Notes;
import com.blb.mapper.NotesMapper;
import com.blb.service.INotesService;
import org.apache.ibatis.annotations.Param;
import org.joda.time.LocalDate;
import org.springframework.web.bind.annotation.*;
import com.blb.utils.ResponseHelp;
import com.blb.utils.UUIDUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhengSanNan
 * @since 2019-12-20
 */
@CrossOrigin
@RestController
@RequestMapping("/notes")
public class NotesController {

    @Resource
    private INotesService notesService;

    @Resource
    private NotesMapper notesMapper;

    /**
     * 文件列表
     *
     * @return
     */
    @GetMapping("notes")
    public List<Notes> notes(String aClass, @RequestParam(required = false, value = "userId") String userId, String token) {
        List<Notes> list = null;
        switch (aClass) {
            case "new":
                list = notesService.list(new QueryWrapper<Notes>().eq("user_id", token));
                break;
            case "favorite":
                Map<String, Object> columnMap = new HashMap<>();
                columnMap.put("user_id", userId);
                columnMap.put("favorite", "2");
                list = notesMapper.selectByMap(columnMap);
                break;
            case "recycle":
                list = notesService.list(new QueryWrapper<Notes>().eq("recycle", "1"));
                break;
            case "share":
                list = notesService.list(new QueryWrapper<Notes>().eq("user_id", userId).eq("share_status", "1"));
                break;
            default:
                list = notesService.list();
        }
       /* //判断md文件/普通文件
        for (Notes note:list) {
            if ("0".equals(note.getType())){
                note.setNotesName(note.getNotesName()+".md");
            }
        }*/
        return list;
    }

    /**
     * 分享查找笔记信息
     *
     * @param nodeId xuyuanxin
     * @return
     */
    @PostMapping("getNotes")
    public Notes getNotes(String nodeId) {
        Notes one = notesService.getOne(new QueryWrapper<Notes>().eq("id", nodeId));
        System.out.println(one);
        return one;
    }

    /**
     * 分享保存笔记
     *
     * @param nodeId xuyuanxin
     */
    @PostMapping("saveNote")
    public ResponseHelp saveNote(String nodeId) {
        System.out.println(nodeId);
        Notes note = notesService.getOne(new QueryWrapper<Notes>().eq("id", nodeId));
        note.setId(UUIDUtils.getId());
        //代表是分享过来的笔记
        note.setShareStatus("1");
        System.out.println(note.getCreateTime());
        notesService.save(note);
        return new ResponseHelp();
    }

    //查找分享笔记
    
    @GetMapping("getMyshare")
    public List<Notes> getMyshare(@RequestParam("userId") String userId) {
        List<Notes> notes = notesService.list(new QueryWrapper<Notes>().eq("user_id", userId).eq("share_status", "1"));
        return notes;
    }

    
    @GetMapping("findFavorite")
    public List<Notes> findFavorite(String userId) {
        return notesService.list(new QueryWrapper<Notes>().eq("user_id",userId).eq("favorite","2"));
    }

    //加星
    @GetMapping("setfavorite")
    public String setfavorite(String id) {
        System.out.println(id);
        Notes notes = new Notes();
        notes.setId(id);
        notes.setFavorite("2");
        notesMapper.updateById(notes);
        return "";
    }

    //取消加星
    @GetMapping("nofavorite")
    public String nofavorite(String id) {
        Notes notes = new Notes();
        notes.setId(id);
        notes.setFavorite("1");
        notesMapper.updateById(notes);
        return "";
    }

    /**
     * 保存
     *
     * @param notes
     */
    @PostMapping("saveNotes")
    public String save(Notes notes) {
        notes.setId(UUIDUtils.getId());
        notes.setNotesName(notes.getNotesName());
        if (notes.getType().equals("1")) {
            notes.setIcon("img/note.png");
        } else if (notes.getType().equals("0")) {
            notes.setIcon("img/markdown.png");
        }

        Date localDate = new Date();
        System.out.println(localDate);
        notes.setCreateTime(localDate);
        notes.setUpdateTime(localDate);

        /**
         * 状态-私有
         * 加密状态-未加密
         * 分享状态-未分享
         * 置顶状态-未置顶
         * 加星状态-未加星
         * 回收状态-未回收
         */
        notes.setStatus("0");
        notes.setEncryption("0");
        notes.setShareStatus("0");
        notes.setSticky("0");
        notes.setFavorite("1");
        notes.setRecycle("0");
        notesService.save(notes);
        return notes.getId();
    }

    
    @RequestMapping("searchNote")
    public Notes searchNote(String id) {
        Notes note = notesService.getById(id);
        System.out.println(note);
        return note;
    }

    /**
     * 修改内容、名称
     *
     * @param id
     * @param content
     * @param notesName
     * @return
     */
    @RequestMapping("updateContent")
    public ResponseHelp updateContent(String id, String content, String notesName) {
        Notes note = notesService.getById(id);
        System.out.println(content);
        note.setContent(content);
        note.setNotesName(notesName);
        System.out.println(note.getContent());
        notesService.updateById(note);
        return ResponseHelp.success();
    }

    @PostMapping("newnote")
    public List<Notes> newnotes(String id) {
        List<Notes> list = notesService.newnote(id);
        System.out.println(list);
        return list;
    }

    @PostMapping("labelnotes")
    public List<Notes> labelnotes(String labelId) {
        List<Notes> list = notesService.getNoteByLabelId(labelId);
        System.out.println(list);
        return list;
    }

    @GetMapping("getNotesList")
    public Map<String, Object> getNoteList(String notesName, String favorite, String userId) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Notes> list = notesService.getNoteListByNotesName(notesName, favorite, userId);
        int count = notesService.getCount(notesName, favorite, userId);
        map.put("data", list);
        map.put("count", count);
        return map;
    }

    /**
     * 删除笔记
     *
     * @param id
     * @return
     */
    
    @GetMapping("deleteNote")
    public ResponseHelp deleteNote(String id) {
        Notes notes = notesService.getById(id);
        if ("0".equals(notes.getRecycle())) {
            notes.setRecycle("1");
            notesService.updateById(notes);
        } else {
            notesService.removeById(id);
        }
        return ResponseHelp.success();
    }

    /**
     * 查找一层笔记
     *
     * @param id
     * @return
     */
    @PostMapping("getallnotes")
    public List<Notes> getallnotes(String id,String token){
        System.out.println(id);
        List<Notes> allNotes;
        if(id==null){
            allNotes = notesService.getAllNotes();
        }else {
            allNotes = notesService.getAllNotesByClassId(id);
        }
        System.out.println(allNotes);
        return allNotes;
    }

    /**
     * 后退
     * @param backId
     * @return
     */
    @PostMapping("backall")
    public List<Notes> backall(String backId,String token){
        System.out.println(backId);
        Notes byId = notesService.getById(backId);
        List<Notes> allNotes = notesService.getAllNotesByClassId(byId.getClassificationId());
        return allNotes;
    }
    @GetMapping("updateName")
    public ResponseHelp updateName(String id, String noteName) {
        Notes notes = notesService.getById(id);
        notes.setNotesName(noteName);
        notesService.updateById(notes);
        System.out.println(notes);
        return ResponseHelp.success();
    }


    @GetMapping("getnOneNote")
    public  List<Notes> getOneNote(String userid,String id){
        List<Notes> oneNote = notesService.getOneNote(userid, id);
        System.out.println(oneNote);
        return oneNote;
    }



    /**
     * 手机端与我分享搜索
     * @param userId
     * @param notesName
     * @return
     */
    @PostMapping("getSearchShare")
    public List<Notes> getSearchShare(String userId ,String notesName){
        System.out.println("aaa");
        System.out.println(userId);
        List<Notes> notes = notesService.list(new QueryWrapper<Notes>().eq("user_id", userId).like("notes_name", notesName).eq("share_status", "1"));
        System.out.println(notes);
        return  notes;
    }

    /**
     * 查询所有加星笔记
     * @param token
     * @return
     */
    @RequestMapping("findAllStar")
    public List<Notes> findAllStar(String token){
        System.out.println(token);
        List<Notes> notes = notesService.list(new QueryWrapper<Notes>().eq("favorite", "2").eq("user_id",token));
        System.out.println(notes);
        return notes;
    }

    /**
     * 根据id查询笔记
     * @param id
     * @return
     */
    @RequestMapping("findNote")
    public Notes findNote(String id){
        Notes notes = notesService.getOne(new QueryWrapper<Notes>().eq("id", id));
        System.out.println(notes);
        return notes;
    }

    /**
     * 修改加星状态
     * @param id
     * @param favorite
     * @return
     */
    @RequestMapping("updateStar")
    public String updateStar(String id,String favorite){
        System.out.println(favorite);
        notesService.updateStars(id,favorite);
        return "true";
    }

    /**
     * 修改加星笔记
     * @param ids
     * @param notesName
     * @param content
     * @return
     */
    @RequestMapping("updateNotes")
    public String updateNotes(String ids,String notesName,String content){
        System.out.println(content);
        Date date = new Date();
        notesService.updateNotes(ids,notesName,content,date);
        return "true";
    }

    /**
     * 模糊查询加星笔记
     * @param userId
     * @param notesName
     * @return
     */
    @RequestMapping("getStarNotes")
    public List<Notes> getStarNotes(String userId,String notesName){
        System.out.println("abc");
        List<Notes> notes = notesService.list(new QueryWrapper<Notes>().like("notes_name", notesName).eq("user_id",userId).eq("favorite","2"));
        return notes;
    }
    /**
     * 标签对应的笔记
     */
    @PostMapping("labelNotes")
    public List<Notes> labelNotes(String labelId){
        System.out.println(labelId);
        List<Notes> noteByLabelId = notesService.getNoteByLabelId(labelId);
        System.out.println(noteByLabelId);
        return noteByLabelId;
    }

    @RequestMapping("sortNotes")
    public List<Notes> sortNotes(String type,String token){
        System.out.println(type);
        List<Notes> sortNotes = notesService.sortNotes(type,token);
        System.out.println(sortNotes);
        return  sortNotes;
    }



    /**
     * 获取所有回收站的笔记
     * @return
     */
    
    @GetMapping("recycle")
    public List<Notes> notes(){
        List<Notes> list = notesService.list(new QueryWrapper<Notes>().eq("recycle", "1"));
        //判断md文件/普通文件
        for (Notes note:list) {
            if (note.getType().equals("0")){
                note.setNotesName(note.getNotesName()+".md");
            }
        }
        return list;
    }


    /**
     *后台接受笔记id，修改回收状态为0
     */
    @RequestMapping("update")
    public void updateNotes(String noteId){
        Notes notes = notesService.getById(noteId);
        notes.setRecycle("0");
        notesService.updateById(notes);
    }

    /**
     * 回收站删除
     */
    @RequestMapping("delete")
    public void deleteNotes(String noteId){
        notesService.removeById(noteId);
    }



}

